package kr.re.etri.snsenabler.client;

import java.util.logging.Level;
import java.util.logging.Logger;

import kr.re.etri.snsenabler.client.i18n.I18NConstants;
import kr.re.etri.snsenabler.client.service.SnsEnablerService;
import kr.re.etri.snsenabler.client.service.SnsEnablerServiceAsync;
import kr.re.etri.snsenabler.shared.Data;
import kr.re.etri.snsenabler.shared.LoginProperties;
import kr.re.etri.snsenabler.shared.ServiceResponse;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Widget;
import com.sencha.gxt.widget.core.client.Window;
import com.sencha.gxt.widget.core.client.box.AlertMessageBox;
import com.sencha.gxt.widget.core.client.event.SelectEvent;
import com.sencha.gxt.widget.core.client.form.PasswordField;
import com.sencha.gxt.widget.core.client.form.TextField;

public class Login {

	private static LoginUiBinder uiBinder = GWT.create(LoginUiBinder.class);

	interface LoginUiBinder extends UiBinder<Widget, Login> {
	}

	private final SnsEnablerServiceAsync snsEnablerService = GWT.create(SnsEnablerService.class);
	private final Logger logger = Logger.getLogger(getClass().getName());

	private final Controller controller;
	private Window window;
	@UiField TextField loginIdTextField;
	@UiField PasswordField passwordTextField;

	public Login(Controller controller) {
		super();

		this.controller = controller;

		window = (Window)uiBinder.createAndBindUi(this);
		window.setHeadingText(I18NConstants.C.loginTitle());
	}

	protected Window getWindow() {
		return window;
	}

	@UiHandler("loginIdTextField")
	public void onUserIdTextFieldEnter(KeyPressEvent event) {
		if (event.getNativeEvent().getKeyCode() == KeyCodes.KEY_ENTER) {
			passwordTextField.focus();
		}
	}

	@UiHandler("passwordTextField")
	public void onPasswordTextFieldEnter(KeyPressEvent event) {
		if (event.getNativeEvent().getKeyCode() == KeyCodes.KEY_ENTER) {
			login();
		}
	}

	@UiHandler("loginButton")
	public void onLoginButtonClicked(SelectEvent event) {
		login();
	}

	private void login() {
		String loginId = loginIdTextField.getText().trim();
		if (loginId.isEmpty()) {
			AlertMessageBox messageBox = new AlertMessageBox("Alert", "Invalid User ID");
			messageBox.show();
			loginIdTextField.focus();
			return;
		}

		String password = passwordTextField.getText().trim();
		if (password.isEmpty()) {
			AlertMessageBox messageBox = new AlertMessageBox("Alert", "Invalid Password");
			messageBox.show();
			passwordTextField.focus();
			return;
		}

		Data loginData = new Data();
		loginData.put(LoginProperties.LOGIN_ID, loginId);
		loginData.put(LoginProperties.LOGIN_PASSWORD, password);

		snsEnablerService.login(loginData, new AsyncCallback<ServiceResponse>() {
			public void onFailure(Throwable caught) {
				logger.log(Level.SEVERE, caught.getMessage(), caught);
				AlertMessageBox messageBox = new AlertMessageBox("Alert", "Access Denied");
				messageBox.show();
			}

			public void onSuccess(ServiceResponse result) {
				if (result.isFailed()) {
					AlertMessageBox messageBox = new AlertMessageBox("Alert", "Login Failed");
					messageBox.show();
				} else {
					controller.setUser(result.getData());
				}
			}
		});
	}
}
